package GradeManager.StudentGradeSystem.service.imp;

import GradeManager.StudentGradeSystem.common.BaseContent;
import GradeManager.StudentGradeSystem.dao.AccountDao;
import GradeManager.StudentGradeSystem.pojo.Account;
import GradeManager.StudentGradeSystem.pojo.LoginDTO;
import GradeManager.StudentGradeSystem.pojo.Result;
import GradeManager.StudentGradeSystem.service.AccountService;
import GradeManager.StudentGradeSystem.utils.CheckCodeUtil;
import GradeManager.StudentGradeSystem.utils.JwtUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class AccountServiceImp implements AccountService {
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public Account checkUserId(Integer userid) {
//        验证用户名是否已存在
        LambdaQueryWrapper<Account> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Account::getUserId,userid);
        return accountDao.selectOne(lqw);
    }
    @Override
    public Result login(Account account) {
        //查找登录的账号密码是否正确
        LambdaQueryWrapper<Account> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Account::getUserId,account.getUserId())
               .eq(Account::getUserPwd,account.getUserPwd());
        Account loginAccount= accountDao.selectOne(wrapper);
        if(loginAccount!=null){
//            保存登录时的账号
            log.info("登录的账号:{}",loginAccount);
            return Result.success(loginAccount);
        }else return Result.error("登录失败！账号或密码错误！");
    }
    @Override
    public void checkCodeImg(HttpServletResponse resp) throws IOException {
        ServletOutputStream os = resp.getOutputStream();
        String checkCode = CheckCodeUtil.outputVerifyImage(100, 50, os, 4);
//        session.setAttribute("checkCode",checkCode);
        //利用redis存储验证码并设置过期时间为2分钟
        log.info("当前验证码为:{}",checkCode);
        stringRedisTemplate.opsForValue().set("checkCode",checkCode,2, TimeUnit.MINUTES);
    }
    @Override
    public boolean addAccount(Account account) {
//      获取seesion中的验证码比对填写的验证码
//        String checkCode = (String)session.getAttribute("checkCode");
        String checkCode = stringRedisTemplate.opsForValue().get("checkCode");
//        如果不相等返回false
        if(!checkCode.equalsIgnoreCase(account.getCheckCode())){
            return false;
        }
//        相等则插入数据库
        log.info("注册的账号:{}",account);
        accountDao.insert(account);
        return true;
    }

    @Override
    public void exitAccount() {
        //退出当前账号
        BaseContent.removeLoginClaims();
    }

    @Override
    public void destroyAccount() {
        //获取当前登录的id
        Integer loginId =(Integer) BaseContent.getLoginClaims().get("loginId");
        //移除已登录的账号
        BaseContent.removeLoginClaims();
        //根据登录的id注销当前账号
        LambdaQueryWrapper<Account> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Account::getUserId,loginId);
        accountDao.delete(wrapper);
    }
}
